/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import Logger from './Logger';

export function stringToUint8Array(str: String): Uint8Array {
  if (str.length === 0) {
    Logger.error(TAG, 'str length is 0');
    return null;
  }

  const len = str.length;
  const tempArray = [];
  for (let i = 0; i < len; ++i) {
    tempArray.push(str.charCodeAt(i));
  }
  return new Uint8Array(tempArray);
}

export const TAG: string = '[CertFramework]';

export const CERT_DATA = new Uint8Array([
  0x30, 0x82, 0x01, 0xb6, 0x30, 0x82, 0x01, 0x1f, 0x02, 0x14, 0x53, 0x19, 0x8b, 0x14, 0x84, 0xb7,
  0xab, 0xca, 0xe2, 0x13, 0x05, 0x16, 0xcb, 0xd6, 0x92, 0x4f, 0x9d, 0x34, 0xa6, 0x76, 0x30, 0x0d,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x1a, 0x31,
  0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c,
  0x65, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x33, 0x30,
  0x31, 0x31, 0x37, 0x31, 0x33, 0x32, 0x35, 0x35, 0x33, 0x5a, 0x17, 0x0d, 0x32, 0x35, 0x31, 0x30,
  0x31, 0x32, 0x31, 0x33, 0x32, 0x35, 0x35, 0x33, 0x5a, 0x30, 0x1a, 0x31, 0x18, 0x30, 0x16, 0x06,
  0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x52, 0x6f,
  0x6f, 0x74, 0x20, 0x43, 0x41, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81,
  0x81, 0x00, 0xe3, 0x65, 0x54, 0x13, 0xc6, 0x07, 0x31, 0x1d, 0x7e, 0xbc, 0xfc, 0x2c, 0x3d, 0xcd,
  0x54, 0x7f, 0x28, 0x05, 0xaa, 0xd6, 0xc1, 0xd0, 0x71, 0xc9, 0x33, 0x46, 0x07, 0x80, 0x6e, 0x5d,
  0xae, 0x9a, 0xcd, 0x29, 0x6b, 0xfb, 0x9b, 0x3a, 0x92, 0xee, 0x2f, 0x96, 0x29, 0x26, 0x12, 0x95,
  0x84, 0x1b, 0x5a, 0x33, 0x8a, 0x28, 0x3c, 0xa3, 0x25, 0xe7, 0x14, 0x00, 0x9f, 0xbb, 0xc2, 0x85,
  0xe6, 0x7f, 0x4d, 0x5c, 0x5e, 0xae, 0x09, 0xb4, 0x9f, 0x8a, 0x23, 0xc0, 0xb4, 0x85, 0xc3, 0x45,
  0xea, 0x82, 0x60, 0x4e, 0x90, 0xbe, 0x6c, 0x0a, 0x0b, 0xb3, 0xfb, 0x04, 0x0d, 0xcc, 0x1d, 0xea,
  0x07, 0x98, 0xa3, 0xf6, 0xdd, 0x4c, 0x16, 0x36, 0x68, 0xf9, 0x10, 0xa4, 0xf3, 0xd1, 0x5f, 0x0c,
  0x59, 0x7b, 0xe6, 0x2e, 0x94, 0xe2, 0x70, 0xbf, 0x23, 0x9d, 0xda, 0x59, 0x05, 0x04, 0xe0, 0x8e,
  0x51, 0x81, 0x02, 0x03, 0x01, 0x00, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x81, 0x81, 0x00, 0xb0, 0x3d, 0xe1, 0xd7, 0x71, 0x9a,
  0x78, 0x85, 0x6e, 0xeb, 0xd5, 0xa7, 0xdf, 0x54, 0x57, 0x55, 0x3a, 0xb9, 0x6c, 0x3a, 0x3a, 0x2c,
  0x03, 0x94, 0x6b, 0x14, 0xc2, 0x55, 0xaa, 0xbb, 0x8f, 0xc8, 0x02, 0x7a, 0x2a, 0x92, 0x5c, 0xca,
  0xf0, 0x57, 0x7b, 0x7a, 0x8b, 0x78, 0xc6, 0x3b, 0x7a, 0x95, 0x27, 0x13, 0x83, 0xb5, 0x5a, 0x84,
  0x55, 0x8d, 0xd8, 0x09, 0x0f, 0x9e, 0x88, 0x69, 0x64, 0x26, 0x5f, 0x46, 0x77, 0xff, 0x67, 0xaa,
  0x71, 0xab, 0x6a, 0x9a, 0x01, 0x16, 0xc0, 0x84, 0x13, 0x90, 0xce, 0x07, 0x30, 0xc5, 0x34, 0x3d,
  0x86, 0x8d, 0xe7, 0x3f, 0xc9, 0x2e, 0x69, 0xac, 0x83, 0x8f, 0xf0, 0x1d, 0x5a, 0x9f, 0x0e, 0x64,
  0x14, 0xd9, 0xb1, 0xa6, 0x19, 0xcf, 0xcc, 0x92, 0x08, 0xbd, 0x94, 0x18, 0xd2, 0x83, 0x6c, 0x13,
  0xb9, 0x45, 0xd8, 0x03, 0x58, 0x2d, 0xf1, 0xd6, 0xb9, 0x66,
]);

export const SIGNATURE_TEXT = new Uint8Array([
  0x77, 0xb9, 0xa2, 0xfd, 0x57, 0x97, 0x66, 0xa2, 0x92, 0x73, 0x67, 0x72, 0x9d, 0x38, 0xa4, 0x72,
  0x76, 0x02, 0x54, 0x6e, 0xeb, 0x83, 0xfd, 0x1d, 0x0c, 0xf1, 0xd7, 0x55, 0x06, 0x69, 0x51, 0xb1,
  0x2f, 0xfb, 0x03, 0x60, 0x68, 0x50, 0x9b, 0x20, 0xef, 0xd4, 0xe9, 0xa2, 0xe8, 0x12, 0xb8, 0x55,
  0x59, 0x8c, 0xfe, 0xfb, 0x00, 0x1d, 0x35, 0x80, 0x0f, 0xa7, 0x1c, 0xea, 0xde, 0xe2, 0x68, 0xe6,
  0x12, 0xd3, 0x3e, 0xcb, 0xe8, 0x29, 0x99, 0xe5, 0x64, 0x2d, 0x99, 0xf5, 0x1b, 0x48, 0xf6, 0x0d,
  0x89, 0x48, 0xeb, 0xe6, 0x12, 0x0a, 0x8d, 0x2d, 0x48, 0xe8, 0xd6, 0x52, 0xb5, 0x5f, 0xc5, 0xe8,
  0xc1, 0x0c, 0x88, 0x5d, 0xa4, 0x57, 0x10, 0xfc, 0x50, 0x94, 0xbb, 0xb6, 0x2a, 0x17, 0xb8, 0xee,
  0xa1, 0xda, 0x0d, 0xf0, 0x63, 0x56, 0xf4, 0xc8, 0x91, 0xb2, 0xbd, 0x47, 0xfe, 0xf1, 0x34, 0x5d
]);

export const STAIN_SIGNATURE_TEXT = new Uint8Array([
  0x00, 0x00, 0x11, 0x12, 0x57, 0x97, 0x61, 0xa2, 0x92, 0x73, 0x67, 0x72, 0x9d, 0x38, 0xa4, 0x72,
  0x76, 0x02, 0x54, 0x6e, 0xeb, 0x83, 0x00, 0x1d, 0x0c, 0xf1, 0xd7, 0x55, 0x06, 0x69, 0x51, 0xb1,
  0x2f, 0xfb, 0x03, 0x60, 0x68, 0x50, 0x9b, 0x20, 0xef, 0xd4, 0xe9, 0x00, 0xe8, 0x12, 0xb0, 0x55,
  0x00, 0x8c, 0xfe, 0xfb, 0x00, 0x11, 0x35, 0x80, 0x0f, 0x00, 0x00, 0x00, 0xde, 0xe6, 0x68, 0x00,
  0x00, 0xd3, 0x36, 0xcb, 0xe8, 0x29, 0x99, 0xe5, 0x64, 0x2d, 0x99, 0xf5, 0x1b, 0x48, 0xf6, 0x0d,
  0x89, 0x40, 0xeb, 0xe6, 0x12, 0x00, 0x8d, 0x2d, 0x40, 0xe8, 0xd6, 0x52, 0xb5, 0x5f, 0xc5, 0xe8,
  0xc1, 0x0c, 0x88, 0x5d, 0xa4, 0x57, 0x10, 0xfc, 0x50, 0x94, 0xbb, 0xb6, 0x2a, 0x17, 0xb8, 0xee,
  0xa1, 0xda, 0x0d, 0xf0, 0x63, 0x56, 0xf4, 0xc8, 0x91, 0xb2, 0x00, 0x47, 0x11, 0xf1, 0x44, 0x00
]);


export const
  CHAIN_DATA_HAS_CRL = '-----BEGIN CERTIFICATE-----\r\n' +
    'MIID6jCCAtKgAwIBAgIIIM2q/TmRoLcwDQYJKoZIhvcNAQELBQAwWjELMAkGA1UE\r\n' +
    'BhMCRU4xEDAOBgNVBAgTB0VuZ2xhbmQxDzANBgNVBAcTBkxvbmRvbjEMMAoGA1UE\r\n' +
    'ChMDdHMyMQwwCgYDVQQLEwN0czIxDDAKBgNVBAMTA3RzMjAeFw0yMzEyMDUwNzM5\r\n' +
    'MDBaFw0yNDEwMzEyMzU5MDBaMGExCzAJBgNVBAYTAkNOMRAwDgYDVQQIEwdKaWFu\r\n' +
    'Z3N1MRAwDgYDVQQHEwdOYW5qaW5nMQwwCgYDVQQKEwN0czMxDDAKBgNVBAsTA3Rz\r\n' +
    'MzESMBAGA1UEAxMJMTI3LjAuMC4xMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\r\n' +
    'CgKCAQEAtt+2QxUevbolYLp51QGcUpageI4fwGLIqv4fj4aoVnHFOOBqVOVpfCLR\r\n' +
    'p26LFV/F8ebwPyo8YEBKSwXzMD1573rMSbaH9BalscH5lZYAbetXoio6YRvzlcmc\r\n' +
    'rVvLBNMeVnxY86xHpo0MTNyP7W024rZsxWO98xFQVdoiaBC+7+midlisx2Y+7u0j\r\n' +
    'zT9GjeUP6JLdLFUZJKUPSTK3jVzw9v1eZQZKYoNfU6vFMd6ndtwW6qEnwpzmmX/U\r\n' +
    'T+p5ThAMH593zszlz330nTSXBjIsGkyvOz9gSB0Z0LAuJj06XUNhGL5xKJYKbdI3\r\n' +
    '8MFQFJKvRHfgTAvVsvAvpBUM2DuBKwIDAQABo4GsMIGpMAkGA1UdEwQCMAAwHQYD\r\n' +
    'VR0OBBYEFDfsHTMZwoA6eaDFlBUyDpka+sYtMAsGA1UdDwQEAwID+DAnBgNVHSUE\r\n' +
    'IDAeBggrBgEFBQcDAQYIKwYBBQUHAwIGCCsGAQUFBwMEMBQGA1UdEQQNMAuCCTEy\r\n' +
    'Ny4wLjAuMTARBglghkgBhvhCAQEEBAMCBkAwHgYJYIZIAYb4QgENBBEWD3hjYSBj\r\n' +
    'ZXJ0aWZpY2F0ZTANBgkqhkiG9w0BAQsFAAOCAQEAp5vTvXrt8ZpgRJVtzv9ss0lJ\r\n' +
    'izp1fJf+ft5cDXrs7TSD5oHrSW2vk/ZieIMhexU4LFwhs4OE7jK6pgI48Dseqxx7\r\n' +
    'B/KktxhVMJUmVXd9Ayjp6f+BtZlIk0cArPuoXToXjsV8caTGBXHRdzxpAk/w9syc\r\n' +
    'GYrbH9TrdNMuTizOb+k268oKXUageZNxHmd7YvOXkcNgrd29jzwXKDYYiUa1DISz\r\n' +
    'DnYaJOgPt0B/5izhoWNK7GhJDy9KEuLURcTSWFysbbnljwO9INPT9MmlS83PdAgN\r\n' +
    'iS8VXF4pce1W9U5jH7d7k0JDVSXybebe1iPFphsZpYM/NE+jap+mPy1nTCbf9g==\r\n' +
    '-----END CERTIFICATE-----\r\n' +
    '-----BEGIN CERTIFICATE-----\r\n' +
    'MIIC0zCCAoWgAwIBAgIIXpLoPpQVWnkwBQYDK2VwMFoxCzAJBgNVBAYTAkVOMRAw\r\n' +
    'DgYDVQQIEwdFbmdsYW5kMQ8wDQYDVQQHEwZMb25kb24xDDAKBgNVBAoTA3RzMTEM\r\n' +
    'MAoGA1UECxMDdHMxMQwwCgYDVQQDEwN0czEwHhcNMjMxMjA1MDczNzAwWhcNMjQw\r\n' +
    'OTAxMjM1OTAwWjBaMQswCQYDVQQGEwJFTjEQMA4GA1UECBMHRW5nbGFuZDEPMA0G\r\n' +
    'A1UEBxMGTG9uZG9uMQwwCgYDVQQKEwN0czIxDDAKBgNVBAsTA3RzMjEMMAoGA1UE\r\n' +
    'AxMDdHMyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtt+2QxUevbol\r\n' +
    'YLp51QGcUpageI4fwGLIqv4fj4aoVnHFOOBqVOVpfCLRp26LFV/F8ebwPyo8YEBK\r\n' +
    'SwXzMD1573rMSbaH9BalscH5lZYAbetXoio6YRvzlcmcrVvLBNMeVnxY86xHpo0M\r\n' +
    'TNyP7W024rZsxWO98xFQVdoiaBC+7+midlisx2Y+7u0jzT9GjeUP6JLdLFUZJKUP\r\n' +
    'STK3jVzw9v1eZQZKYoNfU6vFMd6ndtwW6qEnwpzmmX/UT+p5ThAMH593zszlz330\r\n' +
    'nTSXBjIsGkyvOz9gSB0Z0LAuJj06XUNhGL5xKJYKbdI38MFQFJKvRHfgTAvVsvAv\r\n' +
    'pBUM2DuBKwIDAQABo28wbTAMBgNVHRMEBTADAQH/MB0GA1UdDgQWBBQ37B0zGcKA\r\n' +
    'OnmgxZQVMg6ZGvrGLTALBgNVHQ8EBAMCAQYwEQYJYIZIAYb4QgEBBAQDAgAHMB4G\r\n' +
    'CWCGSAGG+EIBDQQRFg94Y2EgY2VydGlmaWNhdGUwBQYDK2VwA0EAuasLBe55YgvF\r\n' +
    'b4wmHeohylc9r8cFGS1LNQ5UcSn3sGqMYf6ehnef16NLuCW6upHCs8Sui4iAMvsP\r\n' +
    'uKPWR9dKBA==\r\n' +
    '-----END CERTIFICATE-----\r\n' +
    '-----BEGIN CERTIFICATE-----\r\n' +
    'MIIB3zCCAZGgAwIBAgIIWQvOEDl+ya4wBQYDK2VwMFoxCzAJBgNVBAYTAkVOMRAw\r\n' +
    'DgYDVQQIEwdFbmdsYW5kMQ8wDQYDVQQHEwZMb25kb24xDDAKBgNVBAoTA3RzMTEM\r\n' +
    'MAoGA1UECxMDdHMxMQwwCgYDVQQDEwN0czEwHhcNMjMxMjA1MDAwMDAwWhcNMjQx\r\n' +
    'MjA0MjM1OTU5WjBaMQswCQYDVQQGEwJFTjEQMA4GA1UECBMHRW5nbGFuZDEPMA0G\r\n' +
    'A1UEBxMGTG9uZG9uMQwwCgYDVQQKEwN0czExDDAKBgNVBAsTA3RzMTEMMAoGA1UE\r\n' +
    'AxMDdHMxMCowBQYDK2VwAyEAuxadj1ww0LqPN24zr28jcSOlSWAe0QdLyRF+ZgG6\r\n' +
    'klKjdTBzMBIGA1UdEwEB/wQIMAYBAf8CARQwHQYDVR0OBBYEFNSgpoQvfxR8A1Y4\r\n' +
    'St8NjOHkRpm4MAsGA1UdDwQEAwIBBjARBglghkgBhvhCAQEEBAMCAAcwHgYJYIZI\r\n' +
    'AYb4QgENBBEWD3hjYSBjZXJ0aWZpY2F0ZTAFBgMrZXADQQAblBgoa72X/K13WOvc\r\n' +
    'KW0fqBgFKvLy85hWD6Ufi61k4ProQiZzMK+0+y9jReKelPx/zRdCCgSbQroAR2mV\r\n' +
    'xjoE\r\n' +
    '-----END CERTIFICATE-----\r\n';

export const CHAIN_DATA_ROOT = '-----BEGIN CERTIFICATE-----\r\n' +
  'MIIB3zCCAZGgAwIBAgIIWQvOEDl+ya4wBQYDK2VwMFoxCzAJBgNVBAYTAkVOMRAw\r\n' +
  'DgYDVQQIEwdFbmdsYW5kMQ8wDQYDVQQHEwZMb25kb24xDDAKBgNVBAoTA3RzMTEM\r\n' +
  'MAoGA1UECxMDdHMxMQwwCgYDVQQDEwN0czEwHhcNMjMxMjA1MDAwMDAwWhcNMjQx\r\n' +
  'MjA0MjM1OTU5WjBaMQswCQYDVQQGEwJFTjEQMA4GA1UECBMHRW5nbGFuZDEPMA0G\r\n' +
  'A1UEBxMGTG9uZG9uMQwwCgYDVQQKEwN0czExDDAKBgNVBAsTA3RzMTEMMAoGA1UE\r\n' +
  'AxMDdHMxMCowBQYDK2VwAyEAuxadj1ww0LqPN24zr28jcSOlSWAe0QdLyRF+ZgG6\r\n' +
  'klKjdTBzMBIGA1UdEwEB/wQIMAYBAf8CARQwHQYDVR0OBBYEFNSgpoQvfxR8A1Y4\r\n' +
  'St8NjOHkRpm4MAsGA1UdDwQEAwIBBjARBglghkgBhvhCAQEEBAMCAAcwHgYJYIZI\r\n' +
  'AYb4QgENBBEWD3hjYSBjZXJ0aWZpY2F0ZTAFBgMrZXADQQAblBgoa72X/K13WOvc\r\n' +
  'KW0fqBgFKvLy85hWD6Ufi61k4ProQiZzMK+0+y9jReKelPx/zRdCCgSbQroAR2mV\r\n' +
  'xjoE\r\n' +
  '-----END CERTIFICATE-----\r\n';

export const CHAIN_DATA_MAP = {
  CASubject: new Uint8Array([
    0x30, 0x5A, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x45, 0x4E, 0x31,
    0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x07, 0x45, 0x6E, 0x67, 0x6C, 0x61, 0x6E,
    0x64, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x06, 0x4C, 0x6F, 0x6E, 0x64,
    0x6F, 0x6E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x03, 0x74, 0x73, 0x31,
    0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x74, 0x73, 0x31, 0x31, 0x0C,
    0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x03, 0x74, 0x73, 0x31
  ]),
  CAPubKey: new Uint8Array([
    0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x70, 0x03, 0x21, 0x00, 0xBB, 0x16, 0x9D, 0x8F,
    0x5C, 0x30, 0xD0, 0xBA, 0x8F, 0x37, 0x6E, 0x33, 0xAF, 0x6F, 0x23, 0x71, 0x23, 0xA5, 0x49, 0x60,
    0x1E, 0xD1, 0x07, 0x4B, 0xC9, 0x11, 0x7E, 0x66, 0x01, 0xBA, 0x92, 0x52
  ]),
};

export const CHAIN_DATA_CRL = '-----BEGIN X509 CRL-----\r\n' +
  'MIICDTCB9gIBATANBgkqhkiG9w0BAQsFADBaMQswCQYDVQQGEwJFTjEQMA4GA1UE\r\n' +
  'CBMHRW5nbGFuZDEPMA0GA1UEBxMGTG9uZG9uMQwwCgYDVQQKEwN0czIxDDAKBgNV\r\n' +
  'BAsTA3RzMjEMMAoGA1UEAxMDdHMyFw0yMzEyMTUwMjMyMDBaFw0yNDAxMTQwMjMy\r\n' +
  'MDBaMDcwNQIIIM2q/TmRoLcXDTIzMTIxNTAyMzA1M1owGjAYBgNVHRgEERgPMjAy\r\n' +
  'MzEyMTQwMjMwMDBaoC8wLTAfBgNVHSMEGDAWgBQ37B0zGcKAOnmgxZQVMg6ZGvrG\r\n' +
  'LTAKBgNVHRQEAwIBATANBgkqhkiG9w0BAQsFAAOCAQEAomTBBa9igM3xigouO6uh\r\n' +
  'A2P4ws3xr53KYVmpM9nBsuzzzlVBbKh4SbJXboLxFA7NL+FK00lm4is6gQylyPf1\r\n' +
  'rcjgKJx8Ol9n2BfrfH9Jlig4EYD7U/NDFB1S7fTbCbYqztZr0oVEfCwKRfCTTPiT\r\n' +
  'v2a0S4LZylcAdIKzcDUi9bET5d4/NQBVLz1P3gtEQMZAQlh+VNlk80lcSGdCgejz\r\n' +
  'YYbmQ6Lh+AE9QbZMAnCvYD5lT2oU4hUwZcY2ZGhktFnoyFTw80ZjOP/dOwqdkuYi\r\n' +
  'SQhs90WaiBhEGmnau0BcJa6FFShTU0CrxFlx5Q0OvqCDtQuxvoYLLosf021Aw5kp\r\n' +
  'hg==\r\n' +
  '-----END X509 CRL-----\r\n';
